package cn.lili.controller.feign;

import cn.hutool.core.date.DateUtil;
import cn.lili.modules.goods.client.FreeWarehouseClient;
import cn.lili.modules.goods.client.GoodsClient;
import cn.lili.modules.grab.haiguan.service.IFreeWarehouse;
import cn.lili.modules.order.order.client.OrderClient;
import cn.lili.modules.order.order.client.OrderItemClient;
import cn.lili.modules.order.order.entity.constant.FreeWarehouseConstant;
import cn.lili.modules.order.order.entity.dos.Order;
import cn.lili.modules.order.order.entity.dos.OrderItem;
import cn.lili.modules.payment.client.PaymentDataClient;
import cn.lili.modules.payment.entity.dos.PaymentData;
import cn.lili.modules.payment.entity.vo.*;
import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequiredArgsConstructor
@Slf4j
public class FreeWarehouseFeignController implements FreeWarehouseClient {

    private final IFreeWarehouse freeWarehouse;

    @Qualifier("cn.lili.modules.payment.client.PaymentDataClient")
    private final PaymentDataClient paymentDataClient;

    private final OrderClient orderClient;
    private final OrderItemClient orderItemClient;

    @Override
    public Capture179DataResponse capture179Data(Capture179DataVO capture179Data) {
        //组装数据发送给海关抽查
        Capture179DataRequest capture179DataRequest = new Capture179DataRequest();
        capture179DataRequest.setServiceTime(String.valueOf(capture179Data.getServiceTime()));
        capture179DataRequest.setSessionID(capture179Data.getSessionID());
        PayExchangeInfoHead exchangeInfoHead = new PayExchangeInfoHead();
        PaymentData paymentData = paymentDataClient.getPaymentDataBySn(capture179Data.getOrderNo());

        Order bySn = orderClient.getBySn(capture179Data.getOrderNo());
        List<OrderItem> byOrderSn = orderItemClient.getByOrderSn(capture179Data.getOrderNo());


        exchangeInfoHead.setInitalRequest(paymentData != null ? paymentData.getSendBody() : null);
        exchangeInfoHead.setInitalResponse(paymentData != null ? paymentData.getResBody() : null);
        exchangeInfoHead.setEbpCode("460168002H");
        exchangeInfoHead.setPayCode("4403169D3W");
        exchangeInfoHead.setPayTransactionId(bySn.getTransactionId());
        exchangeInfoHead.setTotalAmount(bySn.getFlowPrice());
        exchangeInfoHead.setVerDept("UNIONPAY");
        exchangeInfoHead.setPayType("1");
        exchangeInfoHead.setTradingTime(bySn.getPaymentTime() != null ? DateUtil.format(bySn.getPaymentTime(), "yyyy-MM-dd HH:mm:ss") : null);
        exchangeInfoHead.setNote("");
        capture179DataRequest.setPayExchangeInfoHead(exchangeInfoHead);

        List<PayExchangeInfoBody> exchangeInfoBodyList = new ArrayList<>();
        PayExchangeInfoBody payExchangeInfoBody = new PayExchangeInfoBody();
        List<Goods> goodsList = new ArrayList<>();
        String goodsLink = "https://web-shop-uni.chainlettre05.com/pages/product/goods?id=%s&goodsId=%s";
        for (OrderItem orderItem : byOrderSn) {
            Goods goods = new Goods();
            goods.setGname(orderItem.getGoodsName());
            String format = String.format(goodsLink, orderItem.getSkuId(), orderItem.getGoodsId());
            goods.setItemLink(format);
            goodsList.add(goods);
        }
        payExchangeInfoBody.setGoodsInfo(goodsList);
        payExchangeInfoBody.setRecpAccount("1670029219");
        payExchangeInfoBody.setOrderNo(capture179Data.getOrderNo());
        payExchangeInfoBody.setRecpCode("91460000MAD18XKF5P");
        payExchangeInfoBody.setRecpName("海南永利国际跨境电子商务有限公司");
        exchangeInfoBodyList.add(payExchangeInfoBody);
        capture179DataRequest.setPayExchangeInfoLists(exchangeInfoBodyList);

        log.info("capture179DataRequest == {}", JSONObject.toJSONString(capture179DataRequest));
        JSONObject uploadRes = freeWarehouse.sendCustomsRequest(FreeWarehouseConstant.UPLOAD_179_DATA, capture179DataRequest);
        log.info("capture179Data uploadRes == {}", uploadRes.toJSONString());

        //海关只接受这个返回值,返回其他的,接口校验不通过
        return JSONObject.parseObject("""
                {"code":"10000","message":"上传成功","total":0,"serviceTime":null}
                """, Capture179DataResponse.class);
    }
}
